$(function() {
	initBlockUI();
	updateTable(0, "#pager");
	initPagination();
});

// 初始化分页
function initPagination()
{
	$("#pager").pagination($("#wholepage").val(), {
		items_per_page: 10,
		prev_text: "前一页",
		next_text: "后一页",
		callback: updateTable
	});
}

// 更新表格
function updateTable(page, pager_block)
{
	var url = "del_person.php";
	var pars = { page_status: 2, page: (page + 1) };
	$.post(url, pars, function(xml) {
		$("#tbody2").html("");
		$("item", xml).each(function(i) {
			var id = $("id", this).text();
			var username = $("username", this).text();
			var regtime = $("register_time", this).text().split(" ")[0];
			var deleted = $("deleted", this).text();
			var checked = (deleted == 1) ? " checked=\"checked\" " : "";
			$("<tr></tr>")
				.append($("<th></th>").html(i + 1))
				.append($("<td></td>").html(username))
				.append($("<td></td>").html(regtime))
				.append($("<td></td>")
					.append($("<input type=\"checkbox\" " + checked + " />")
						.attr("userid", id)
						.click(function() {
							realDelOrNot($(this).attr("userid"), this.checked);
						})
					)
					.append(" 删除？")
				)
				.append($("<td></td>").attr("id", "status_" + id))
			.appendTo("#tbody2");
		});
		tablePretty("#tbody2");
	});
	return false;
}

// 是否删除选项框
function realDelOrNot(id, check)
{
	var del = check ? 1 : 0;
	var str_success = check ? "用户删除成功！" : "用户恢复成功！";
	var str_error = check ? "用户删除失败！" : "用户恢复失败！";
	var url = "del_person.php";
	var pars = { page_status: 1, id: id, del: del };
	$.post(url, pars, function(text) {
		if (/success/.test(text)) { $("#status_" + id).html($("<span></span>").html(str_success).css({color: "green"}).fadeOut(6000)); }
		else { $("#status_" + id).html($("<span></span>").html(str_error).css({color: "red"}).fadeOut(6000)); }
	});
}
